/*
 * Copyright (c) 2012, Markus Weber All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of
 * conditions and the following disclaimer. Redistributions in binary form must reproduce the
 * above copyright notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * Neither the name of the author nor the names of its contributors may be used to endorse or
 * promote products derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 */
package de.dfki.graph.index;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * Index for efficient retrieval.
 *
 * @author Markus Weber
 *
 */
public class Index implements Serializable {

    /**
     * Generated serial ID
     */
    private static final long serialVersionUID = -8239646650983472726L;
    /**
     * Index
     */
    private Map<Byte, Index> index;
    /**
     * Pointer to nodes.
     */
    private Node pointer;

    /**
     * Creates a new Index node.
     */
    public Index() {
        super();
        index = new HashMap<Byte, Index>();
        pointer = null;
    }

    /**
     * Adds a sequence.
     *
     * @param seq
     * @param n
     * @param floorplan
     */
    public void addNode(byte[] seq, Node n) {
        addNode(seq, n, 0);
    }

    /**
     * Adds a sequence
     *
     * @param seq
     * @param n
     * @param floorplan
     * @param pos
     */
    public void addNode(byte[] seq, Node n, int pos) {
        // termination criterion for recursive call
        if (pos > seq.length) {
            return;
        }
        // we are at the last position. We need to add the pointer to the next
        // node
        if (pos == seq.length) {
            this.pointer = n;
            return;
        }
        if (!index.containsKey(seq[pos])) {
            Index i = new Index();
            index.put(seq[pos], i);
        }
        index.get(seq[pos]).addNode(seq, n, ++pos);
    }

    /**
     *
     * @param sequence
     * @return
     */
    public Node getBestMatch(byte[] sequence) {
        return getBestMatch(sequence, 0);
    }

    /**
     * Best match with recursion
     *
     * @param sequence
     * @param pos
     * @return
     */
    public Node getBestMatch(byte[] sequence, int pos) {
        if (pos == sequence.length) {
            return pointer;
        } else if (pos > sequence.length) {
            return null;
        }
        Index i = index.get(sequence[pos]);
        if (i != null) { // check for exact match
            return i.getBestMatch(sequence, ++pos);
        }
        // similarity approach
        double best = 0.0;
        for (byte b : this.index.keySet()) {
            double similarity = compare(sequence[pos], b);
            if (similarity > best) {
                i = this.index.get(b);
                best = similarity;
            }
        }
        if (i == null) {
            return null;
        }
        return i.getBestMatch(sequence, ++pos);

    }

    /**
     * Compares two values.
     *
     * @param query
     * @param database
     * @return
     */
    private double compare(byte query, byte database) {
        return 0.05;
    }

    /**
     * Returns an exact match for the sequence.
     *
     * @param sequence
     * @return
     */
    public Node getExactMatch(byte[] sequence) {
        return getExactMatch(sequence, 0);
    }

    /**
     *
     * @param sequence
     * @param pos
     * @return
     */
    public Node getExactMatch(byte[] sequence, int pos) {
        if (pos == sequence.length) {
            return pointer;
        } else if (pos > sequence.length) {
            return null;
        }
        Index i = index.get(sequence[pos]);
        if (i != null) { // check for exact match
            return i.getExactMatch(sequence, ++pos);
        }
        return null;
    }
}
